using System.Web.Mvc;
using good1.Core;
using SharpArch.Core.PersistenceSupport;
using System.Collections.Generic;
using SharpArch.Web.NHibernate;
using SharpArch.Web.CommonValidator;
using SharpArch.Core;

namespace good1.Web.Controllers
{
    [HandleError]
    public class ThirdPartyIdentityController : Controller
    {
        public ThirdPartyIdentityController(IRepository<ThirdPartyIdentity> thirdPartyIdentityRepository)
        {
            Check.Require(thirdPartyIdentityRepository != null, "thirdPartyIdentityRepository may not be null");

            this.thirdPartyIdentityRepository = thirdPartyIdentityRepository;
        }

        [Transaction]
        public ActionResult Index() {
            IList<ThirdPartyIdentity> thirdPartyIdentitys = thirdPartyIdentityRepository.GetAll();
            return View(thirdPartyIdentitys);
        }

        [Transaction]
        public ActionResult Show(int id) {
            ThirdPartyIdentity thirdPartyIdentity = thirdPartyIdentityRepository.Get(id);
            return View(thirdPartyIdentity);
        }

        public ActionResult Create() {
           return View();
          }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(ThirdPartyIdentity thirdPartyIdentity)
        {
            if (thirdPartyIdentity.IsValid())
            {
                thirdPartyIdentityRepository.SaveOrUpdate(thirdPartyIdentity);

                TempData["message"] = "The project was successfully created.";
                return RedirectToAction("Index");
            }

            MvcValidationAdapter.TransferValidationMessagesTo(ViewData.ModelState,
                thirdPartyIdentity.ValidationResults());
            return View();
        }

        [Transaction]
        public ActionResult Edit(int id) {
            ThirdPartyIdentity thirdPartyIdentity = thirdPartyIdentityRepository.Get(id);
            return View(thirdPartyIdentity);
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, [ModelBinder(typeof(DefaultModelBinder))] ThirdPartyIdentity thirdPartyIdentity)
        {
            ThirdPartyIdentity thirdPartyIdentityToUpdate = thirdPartyIdentityRepository.Get(id);
            TransferFormValuesTo(thirdPartyIdentityToUpdate, thirdPartyIdentity);

            if (thirdPartyIdentityToUpdate.IsValid()) {
                TempData["message"] = "The project was successfully updated.";
                return RedirectToAction("Index");
            }
            else {
                thirdPartyIdentityRepository.DbContext.RollbackTransaction();
                MvcValidationAdapter.TransferValidationMessagesTo(ViewData.ModelState, 
                    thirdPartyIdentityToUpdate.ValidationResults());
                return View(thirdPartyIdentityToUpdate);
            }
        }

        private void TransferFormValuesTo(ThirdPartyIdentity thirdPartyIdentityToUpdate, ThirdPartyIdentity thirdPartyIdentityForm)
        {
            thirdPartyIdentityToUpdate.ClaimedIdentifier = thirdPartyIdentityForm.ClaimedIdentifier;
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id) {
            string resultMessage = "The thirdPartyIdentity was successfully deleted.";
            ThirdPartyIdentity thirdPartyIdentityToDelete = thirdPartyIdentityRepository.Get(id);

            if (thirdPartyIdentityToDelete != null)
            {
                thirdPartyIdentityRepository.Delete(thirdPartyIdentityToDelete);

                try {
                    thirdPartyIdentityRepository.DbContext.CommitChanges();
                }
                catch {
                    resultMessage = "A problem was encountered preventing the project from being deleted. " +
						"Another item likely depends on this project.";
                    thirdPartyIdentityRepository.DbContext.RollbackTransaction();
                }
            }
            else {
                resultMessage = "The project could not be found for deletion. It may already have been deleted.";
            }

            TempData["Message"] = resultMessage;
            return RedirectToAction("Index");
        }

        private readonly IRepository<ThirdPartyIdentity> thirdPartyIdentityRepository;
    }
}
